package com.bingxue.edu.management.education.service;

import com.bingxue.edu.common.constant.AuthConstants;
import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.management.education.model.query.ClassScheduleQuery;
import com.bingxue.edu.management.education.model.req.ClassroomScheduleReq;
import com.bingxue.edu.management.education.model.resp.ClassroomScheduleInfoResp;
import com.bingxue.edu.management.education.model.resp.ClassroomScheduleResp;
import com.bingxue.edu.management.education.model.resp.schedule.ClassScheduleResp;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class ClassScheduleService {

    private final ClassInfoService classInfoService;

    private final LessonService lessonService;

    public ClassroomScheduleResp getClassroomScheduleTable(ClassroomScheduleReq req) {
        long between = ChronoUnit.DAYS.between(req.getStartTime(), req.getEndTime());
        ClassroomScheduleResp classroomScheduleResp = new ClassroomScheduleResp();
        // 查询课次信息
        List<ClassroomScheduleInfoResp> list = lessonService.getLessonInfoByLocalDate(req);
        // 排序
        list.sort(Comparator.comparing(ClassroomScheduleInfoResp::getLessonDate,Comparator.naturalOrder()));
        // 提取
        if(AuthConstants.MONTH.equals(req.getDateFlag())){
            classroomScheduleResp.setMonthMap(mapComparingByKeyForLocalDate(list));
        }
        if(AuthConstants.WEEK.equals(req.getDateFlag())){
            ValidationUtils.throwIf(between > 7,"请传一周的日期");
            // 设置初始值
            LinkedHashMap<String, LinkedHashMap<LocalDate,List<ClassroomScheduleInfoResp>>> listLinkedHashMap = new LinkedHashMap<>();
            Map<String, List<ClassroomScheduleInfoResp>> map = list.stream().collect(Collectors.groupingBy(ClassroomScheduleInfoResp::getClassroomAndCampusName));
            // 排序key
            LinkedHashMap<String, List<ClassroomScheduleInfoResp>> collect = mapComparingByKeyForString(map);
            collect.forEach((key,value)->{
                listLinkedHashMap.put(key,mapComparingByKeyForLocalDate(value));
            });
            classroomScheduleResp.setWeekMap(listLinkedHashMap);
        }
        if(AuthConstants.DAY.equals(req.getDateFlag())){
            ValidationUtils.throwIf(between > 1,"请传当前的日期");
            // 设置初始值
            LinkedHashMap<String, LinkedHashMap<String,List<ClassroomScheduleInfoResp>>> listLinkedHashMap = new LinkedHashMap<>();

            LinkedHashMap<String, List<ClassroomScheduleInfoResp>> collect = list.stream()
                    .collect(Collectors.groupingBy(ClassroomScheduleInfoResp::getBetween))
                    .entrySet().stream()
                    .collect(Collectors.toMap(
                            Map.Entry::getKey,
                            Map.Entry::getValue,
                            (e1, e2) -> e1,
                            LinkedHashMap::new
                    ));

            collect.forEach((key,value)->{
                LinkedHashMap<String, List<ClassroomScheduleInfoResp>> collect1 = value.stream().collect(Collectors.groupingBy(ClassroomScheduleInfoResp::getClassroomAndCampusName))
                        .entrySet().stream()
                        .collect(Collectors.toMap(
                                Map.Entry::getKey,
                                Map.Entry::getValue,
                                (e1, e2) -> e1,
                                LinkedHashMap::new
                        ));
                listLinkedHashMap.put(key,collect1);
            });

            //排序
            classroomScheduleResp.setDayMap(listLinkedHashMap);
        }
        return classroomScheduleResp;
    }

    public LinkedHashMap<LocalDate, List<ClassroomScheduleInfoResp>> mapComparingByKeyForLocalDate(List<ClassroomScheduleInfoResp> list){
        Map<LocalDate, List<ClassroomScheduleInfoResp>> map = list.stream().collect(Collectors.groupingBy(ClassroomScheduleInfoResp::getLessonDate));
        //排序key
        return map.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (e1, e2) -> e1,
                        LinkedHashMap::new
                ));
    }

    public LinkedHashMap<String, List<ClassroomScheduleInfoResp>> mapComparingByKeyForString(Map<String, List<ClassroomScheduleInfoResp>> map){
        //排序key
        return map.entrySet().stream()
                .sorted(Map.Entry.comparingByKey())
                .collect(Collectors.toMap(
                        Map.Entry::getKey,
                        Map.Entry::getValue,
                        (e1, e2) -> e1,
                        LinkedHashMap::new
                ));
    }

    public List<ClassScheduleResp> getClassScheduleTable(ClassScheduleQuery query) {
        QueryWrapper queryWrapper = QueryBuilder.build(query);
        return classInfoService.getMapper().selectListWithRelationsByQueryAs(queryWrapper, ClassScheduleResp.class);
    }
}
